package com.utf999.qiangmanger.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.utf999.qiangmanger.dao.AbstractRelationalDBDAO;
import com.utf999.qiangmanger.dao.iface.IOrderInfoDAO;
import com.utf999.qiangmanger.entity.PageEntity;
import com.utf999.qiangmanger.entity.orm.OrderInfoEntity;
import com.utf999.qiangmanger.exception.BizException;

/**
 * @author yinwenjie
 *
 */
@Component("OrderInfoDAOImpl")
public class OrderInfoDAOImpl extends AbstractRelationalDBDAO<OrderInfoEntity> implements IOrderInfoDAO {

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.AbstractRelationalDBDAO#getEntityClass()
	 */
	@Override
	protected Class<OrderInfoEntity> getEntityClass() {
		return OrderInfoEntity.class;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderInfoDAO#appendOrderInfoGroupMapping(java.lang.String, java.lang.String[])
	 */
	@Override
	public void appendOrderInfoGroupMapping(String orderinfoID, String[] groupid) throws BizException {
		for (String groupidItem : groupid) {
			HashMap<String, Object> condition = new HashMap<String, Object>();
			condition.put("orderinfoid", orderinfoID);
			condition.put("usergroupid", groupidItem);
			this.executeSQLFile("IOrderInfoDAO.appendOrderInfoGroupMapping", condition);
		}
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderInfoDAO#updatePickingStatus(java.lang.String, java.lang.Float)
	 */
	//TODO 未进行SQL update语句的优化
	@Override
	public void updatePickingStatus(String orderinfoid, Float totalAmount) throws BizException {
		OrderInfoEntity orderinfo = this.getEntity(orderinfoid);
		orderinfo.setPickingStatus(1);
		orderinfo.setTotalAmount(totalAmount);
		
		this.update(orderinfo);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderInfoDAO#queryPickingOrder(java.lang.String)
	 */
	//TODO 未进行sql语句的索引优化
	@Override
	public List<OrderInfoEntity> queryPickingOrder(String storefrontid) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("storefrontid", storefrontid);
		
		List<OrderInfoEntity> orders = this.queryByHqlFile("IOrderInfoDAO.queryPickingOrder", condition);
		return orders;
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderInfoDAO#queryTaskOrder(java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String)
	 */
	//TODO 没有对sql索引进行优化
	@Override
	public List<OrderInfoEntity> queryTaskOrder(String storefrontid, Integer orderMode, Integer orderStatus, String waiterCode, String roomNo) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("storefrontid", storefrontid);
		if(orderMode != null) {
			condition.put("orderMode", orderMode);
		}
		if(orderStatus != null) {
			condition.put("orderStatus", orderStatus);
		}
		if(!StringUtils.isEmpty(waiterCode)) {
			condition.put("waiterCode", waiterCode);
		}
		if(!StringUtils.isEmpty(roomNo)) {
			condition.put("roomNo", roomNo);
		}
		
		//根据orderStatus参数的情况，决定执行那一句HQL
		List<OrderInfoEntity> orders = null;
		if(orderStatus != null) {
			orders = this.queryByHqlFile("IOrderInfoDAO.queryTaskOrderInclueStatus", condition);
		} else {
			orders = this.queryByHqlFile("IOrderInfoDAO.queryTaskOrder", condition);
		}
		return orders;
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderInfoDAO#queryAccountOrder(java.lang.String, java.lang.Integer, java.lang.String, java.lang.String)
	 */
	//TODO 没有对sql索引进行优化
	@Override
	public List<OrderInfoEntity> queryAccountOrders(String storefrontid, Integer orderMode, String waiterCode, String roomNo) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("storefrontid", storefrontid);
		if(orderMode != null) {
			condition.put("orderMode", orderMode);
		}
		if(!StringUtils.isEmpty(waiterCode)) {
			condition.put("waiterCode", waiterCode);
		}
		if(!StringUtils.isEmpty(roomNo)) {
			condition.put("roomNo", roomNo);
		}
		
		List<OrderInfoEntity> orders = null;
		orders = this.queryByHqlFile("IOrderInfoDAO.queryAccountOrders", condition);
		return orders;
	}
	
	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderInfoDAO#queryAccountOrder(java.lang.String)
	 */
	//TODO 没有对sql索引进行优化
	@Override
	public OrderInfoEntity queryAccountOrder(String orderinfoid) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("orderinfoid", orderinfoid);
		
		List<OrderInfoEntity> orders = null;
		orders = this.queryByHqlFile("IOrderInfoDAO.queryAccountOrder", condition);
		if(orders == null || orders.size() == 0) {
			return null;
		}
		
		return orders.get(0);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderInfoDAO#queryOrderinfoDetails(java.lang.String)
	 */
	@Override
	public OrderInfoEntity queryOrderinfoDetails(String orderinfoid) throws BizException {
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("orderinfoid", orderinfoid);
		
		List<OrderInfoEntity> orders = this.queryByHqlFile("IOrderInfoDAO.queryOrderinfoDetails", condition);
		if(orders == null || orders.size() == 0) {
			return null;
		}
		
		return orders.get(0);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderInfoDAO#saveOrderinfoAccount(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Float)
	 */
	@Override
	public void saveOrderinfoAccount(String orderinfoid, String adminuserid, Integer realIntegral, Float realTotalAmount) throws BizException {
		//外面的service层已经完成了判断，这里不需要在进行有效性判断了
		HashMap<String, Object> condition = new HashMap<String, Object>();
		condition.put("orderinfoid", orderinfoid);
		condition.put("accountAdmin", adminuserid);
		condition.put("realIntegral", realIntegral);
		condition.put("filingTime", new Date());
		condition.put("realTotalAmount", realTotalAmount);
		
		this.executeSQLFile("IOrderInfoDAO.saveOrderinfoAccount", condition);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderInfoDAO#reportOrderInfo(java.lang.String, java.lang.String, java.lang.Integer, java.lang.String, java.lang.String, java.util.Date, java.util.Date)
	 */
	//TODO 没有经过SQL索引优化
	@Override
	public PageEntity reportOrderInfo(String companyid,
			String storefrontid, Integer orderMode, String workNumber,
			String roomNo, Date beginFilingTime, Date endFilingTime,
			Integer pageNum , Integer maxrow) throws BizException {
		
		HashMap<String, Object> condition = new HashMap<String, Object>();
		if(!StringUtils.isEmpty(companyid)) {
			condition.put("companyid", companyid);
		}
		if(!StringUtils.isEmpty(storefrontid)) {
			condition.put("storefrontid", storefrontid);
		}
		if(orderMode != null) {
			condition.put("orderMode", orderMode);
		}
		if(!StringUtils.isEmpty(workNumber)) {
			condition.put("workNumber", workNumber);
		}
		if(!StringUtils.isEmpty(roomNo)) {
			condition.put("roomNo", roomNo);
		}
		if(beginFilingTime != null) {
			condition.put("beginFilingTime", beginFilingTime);
		}
		if(endFilingTime != null) {
			condition.put("endFilingTime", endFilingTime);
		}
		
		//分页信息在这里
		if(pageNum == null) {
			pageNum = 0;
		}
		if(maxrow == null) {
			maxrow = 20;
		}
		return this.queryByPageHQLFile("IOrderInfoDAO.reportOrderInfo", condition, pageNum, maxrow);
	}

	/* (non-Javadoc)
	 * @see com.utf999.qiangmanger.dao.iface.IOrderInfoDAO#reportTotalGroupbyUserid(java.lang.String, java.lang.String, java.lang.Integer, java.lang.String, java.lang.String, java.util.Date, java.util.Date, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public PageEntity reportTotalGroupbyUserid(String companyid,
			String storefrontid, Integer orderMode, String workNumber,
			String roomNo, Date beginFilingTime, Date endFilingTime,
			Integer pageNum, Integer maxrow) throws BizException {
		
		HashMap<String, Object> condition = new HashMap<String, Object>();
		if(!StringUtils.isEmpty(companyid)) {
			condition.put("companyid", companyid);
		}
		if(!StringUtils.isEmpty(storefrontid)) {
			condition.put("storefrontid", storefrontid);
		}
		if(orderMode != null) {
			condition.put("orderMode", orderMode);
		}
		if(!StringUtils.isEmpty(workNumber)) {
			condition.put("workNumber", workNumber);
		}
		if(!StringUtils.isEmpty(roomNo)) {
			condition.put("roomNo", roomNo);
		}
		if(beginFilingTime != null) {
			condition.put("beginFilingTime", beginFilingTime);
		}
		if(endFilingTime != null) {
			condition.put("endFilingTime", endFilingTime);
		}
		
		//分页信息在这里
		if(pageNum == null) {
			pageNum = 0;
		}
		if(maxrow == null) {
			maxrow = 20;
		}
		return this.queryByPageSQLFile("IOrderInfoDAO.reportTotalGroupbyUserid", condition, pageNum, maxrow);
	}
}
